package com.hatricks.is.base.completablefututure;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @packageName: com.xh.completablefututure
 * @className: CompletableFutureTest
 * @author: hatricks
 * @date: 2022/3/31 15:21
 * @description:
 */
public class CompletableFutureTest {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(2);
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            System.out.println(StrUtil.format("当前线程的名称:{},睡2s", Thread.currentThread().getName()));
            ThreadUtil.safeSleep(2000);
            System.out.println("supplyAsync" + Thread.currentThread().getName());
            return 1;
        }, executor).thenApplyAsync(s -> {
            System.out.println(s + "world");
            return "ss.json";
        }, executor);

        future.thenRunAsync(() -> {
            System.out.println("future的异步调用");
        });
        future.thenRun(() -> {
            System.out.println("future的同步调用");
        });


//        第一次commit
    }
}
